﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using UL360Data;
using UL360SurveyCommon;

namespace UL360ConsoleTest
{
    class Program
    {
        static void Main(string[] args)
        {
           /* string url = URLEncrypt.Encrypt("1630");

            System.IO.StreamWriter sw = new System.IO.StreamWriter(@"C:\temp\url.txt");
            sw.WriteLine(url);
            sw.Close();
            
            string[] firstNames = { "Anders", "Johan", "Sara", "Lisa", "Bert", "Jonas", "Malin", "Ahmed", "Santi", "Matti" };
            string[] lastNames = { "Johnsson", "Larsson", "Bengtsson", "Olofsson", "Olsson", "Ali", "Jones", "Ling", "Mäntynen", "Eklund" };
            string[] organisations = { "Testkurs 1", "Testkurs 2", "Testkurs 3", "Testkurs 4", "Testkurs 5" };
            //string[] mails = { "mariakallin@hotmail.com" };
            int[] spraks = { 1, 2, 3 };

            DateTime seed = DateTime.Now;

            using (var db = new UL360Entities())
            {
                Random rand = new Random(seed.Millisecond);
                
                for (int i = 0; i < 100; i++)
                {
                    string firstName = firstNames[rand.Next(0, (firstNames.Length - 1))];
                    string lastName = lastNames[rand.Next(0, (lastNames.Length - 1))];
                    Guid g = Guid.NewGuid();
                    Guid app = new Guid("0647023E-638F-46FF-AFF8-C12AB276A04E");
                    string mail = "mariakallin@hotmail.com";

                     if (rand.Next(0, 100) <= 50)
                    {
                        mail = mails[0];
                    }
                    else
                    {
                        mail = mails[0];
                    } 

                    aspnet_Users usr = new aspnet_Users
                    {
                        ApplicationId = app,
                        UserId = g,
                        UserName = firstName + "_" +lastName,
                        LoweredUserName = firstName + "_" + lastName,
                        IsAnonymous = false,
                        LastActivityDate = DateTime.Now
                    };

                    aspnet_Membership mem = new aspnet_Membership
                    {
                        UserId = g,
                        Password = "hej",
                        Email = mail,
                        ApplicationId = app,
                        PasswordFormat = 0,
                        PasswordSalt = "asd",
                        LastPasswordChangedDate = DateTime.Now,
                        FailedPasswordAttemptCount = 0,
                        FailedPasswordAnswerAttemptWindowStart = DateTime.Now,
                        FailedPasswordAttemptWindowStart = DateTime.Now,
                        IsApproved = true,
                        IsLockedOut = false,
                        CreateDate = DateTime.Now,
                        LastLoginDate = DateTime.Now,
                        LastLockoutDate = DateTime.Now
                    };

                    Handledare hand = new Handledare
                    {
                        handledarid = g,
                        efternamn = lastName,
                        fornamn = firstName,
                        aktiv = true
                    };

                    db.aspnet_Users.Add(usr);

                    db.SaveChanges();
                    
                    db.aspnet_Membership.Add(mem);

                    db.SaveChanges();

                    db.Handledares.Add(hand);

                    db.SaveChanges(); 

                    db.Kurstillfalles.Add(new Kurstillfalle
                    {
                        organisation = organisations[rand.Next(0, (organisations.Length - 1))],
                        startdatum = DateTime.Now.AddDays(-33),
                        slutdatum = DateTime.Now.AddDays(-30),
                        blankettsetid = 1,
                        sprakid = spraks[rand.Next(0, (spraks.Length - 1))],
                        forganisation = "forganisation",
                        fpostnr = "123213",
                        fpostort = "Sundsvall",
                        fgatuadress = "test",
                        ansvarigHL = g,
                        aktiv = true
                    }); 

                    try
                    {
                        db.SaveChanges();
                    }
                    catch (Exception exc)
                    {
                        string hej = "";
                    }
                }

                List<Handledare> allHandledare = db.Handledares.ToList();
                foreach (Kurstillfalle k in db.Kurstillfalles)
                {
                    List<Handledare> handledare = new List<Handledare>();
                    for(int i = 0; i < 4; i++)
                    {
                        handledare.Add(allHandledare.OrderBy(x => rand.Next()).Take(5).First());
                    }

                    foreach (Handledare h in handledare)
                    {
                        db.Kurstillfalles.Where(c => c.kursid == k.kursid).First().Handledares.Add(h);
                    }
                }
                db.SaveChanges(); 

               List<Kurstillfalle> courses = db.Kurstillfalles.ToList();
                for (int i = 0; i < 50; i++)
                {
                    string firstName = firstNames[rand.Next(0, (firstNames.Length - 1))];
                    string lastName = lastNames[rand.Next(0, (lastNames.Length - 1))];
                    Kurstillfalle course = courses.OrderBy(x => rand.Next()).Take(5).First();
                    string mail = "mariakallin@hotmail.com";

                    if (rand.Next(0, 100) <= 50)
                    {
                        mail = mails[0];
                    }
                    else
                    {
                        mail = mails[0];
                    } 

                    Deltagare d = new Deltagare
                    {
                        efternamn = lastName,
                        fornamn = firstName,
                        organisation = organisations[rand.Next(0, (organisations.Length - 1))],
                        email = mail,
                        sprakid = spraks[rand.Next(0, (spraks.Length - 1))],
                        q_string = rand.Next(0,999999999).ToString(),
                        kursid = course.kursid
                    };

                    db.Deltagares.Add(d);

                    db.SaveChanges(); 
                }
            } */
        }
    }
}
